package service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.MerchantAuditDao;
import po.MerchantAudit;
import service.MerchantAuditManager;
import service.WebService;
import vo.MerchantAuditVo;
import vo.Merchant;

@Service("merchantAuditManager")
public class MerchantAuditManagerImpl implements MerchantAuditManager {

	@Autowired
	private MerchantAuditDao mad;
	@Autowired
	private WebService ws;
	
	@Override
	public List<MerchantAuditVo> listMerchantAudit() {
		// TODO Auto-generated method stub
		List<MerchantAudit> merchantAudits = mad.listMerchantAudits();
		List<String> midList = new ArrayList<String>();
		List<MerchantAuditVo> merchantList= new ArrayList<MerchantAuditVo>();
		for (MerchantAudit merchantAudit : merchantAudits) {			
			midList.add(merchantAudit.getmId());
		}
		Map<String, Merchant> map = ws.listAllMerchant();
		for (MerchantAudit merchantAudit : merchantAudits) {			
			merchantList.add(new MerchantAuditVo(map.get(merchantAudit.getmId()), merchantAudit.getStatus(), merchantAudit.getCreateDate()));
		}
		return merchantList;
	}

	@Override
	public MerchantAudit findMerchantAuditById(String id) {
		// TODO Auto-generated method stub
		return mad.findMerchantAuditById(id);
	}

	@Override
	public String addMerchantAudit(MerchantAudit merchantAudit) {
		// TODO Auto-generated method stub
		return mad.insertMerchantAudit(merchantAudit);
	}

	@Override
	public String auditMerchant(String mId,int status) {
		// TODO Auto-generated method stub
		MerchantAudit merchantAudit = mad.findMerchantAuditById(mId);
		if(merchantAudit != null){			
			merchantAudit.setStatus(status);
			return mad.updateMerchantAudit(merchantAudit);
		}else {
			return null;
		}
	}

	@Override
	public List<String> listBlackMerchant() {
		// TODO Auto-generated method stub
		return mad.listBlackMerchant();
	}

	@Override
	public int checkMerchantStatus(String mId) {
		// TODO Auto-generated method stub
		MerchantAudit merchantAudit = mad.findMerchantAuditById(mId);
		if(merchantAudit != null){			
			return merchantAudit.getStatus();
		}else {
			return 0;
		}
	}
	
}
